package krow.dev.addetector.view;

import java.util.ArrayList;
import java.util.List;

import krow.dev.addetector.control.Module;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class LinkableTextView extends TextView {

	private OnLinkClickListener mLinkClickListener;

	public LinkableTextView(Context context) {
		super(context);
	}

	public LinkableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LinkableTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Object object = getText();
		if(!(object instanceof Spanned)){
			return false;
		}
		Spanned spanned = (Spanned) object;

		int action = event.getAction();
		if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
			int x = (int) event.getX();
			int y = (int) event.getY();

			x -= getTotalPaddingLeft();
			y -= getTotalPaddingTop();

			x += getScrollX();
			y += getScrollY();

			Layout layout = getLayout();
			int line = layout.getLineForVertical(y);
			int offset = layout.getOffsetForHorizontal(line, x);

			ClickableSpan[] link = spanned.getSpans(offset, offset, ClickableSpan.class);
			if (link.length != 0) {
				if (action == MotionEvent.ACTION_UP) {
					link[0].onClick(this);
				} else if (action == MotionEvent.ACTION_DOWN) {
//						Selection.setSelection(spanned, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
				}
				return true;
			}
		}


		return false;

	}

	public void gatherLinksForText(List<Module> moduleList) {
		List<Hyperlink> hyperLinkList = new ArrayList<Hyperlink>();
		StringBuilder stringBuilder = new StringBuilder();
		
		int index = 0;
		for (int i = 0; i < moduleList.size(); i++) {
			Hyperlink hyperlink = new Hyperlink();

			Module module = moduleList.get(i);
			String tag = module.toString();

			hyperlink.span = new InternalURLSpan(module);
			hyperlink.start = index;
			hyperlink.end = index + tag.length();

			stringBuilder.append(tag);

			index = hyperlink.end;
			if (i < moduleList.size() - 1) {
				stringBuilder.append(", ");
				index += 2;
			}
			hyperLinkList.add(hyperlink);
		}

		SpannableString spannableString = new SpannableString(stringBuilder.toString());

		for (int i = 0; i < hyperLinkList.size(); i++) {
			Hyperlink hyperlink = hyperLinkList.get(i);
			spannableString.setSpan(hyperlink.span, hyperlink.start, hyperlink.end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		setText(spannableString);
	}

	public void setOnLinkClickListener(OnLinkClickListener linkClickListener) {

		mLinkClickListener = linkClickListener;
	}

	public class InternalURLSpan extends ClickableSpan {
		private Module mModule;

		public InternalURLSpan(Module module) {
			mModule = module;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			ds.setColor(ds.linkColor);
			ds.setUnderlineText(true);
		}

		@Override
		public void onClick(View textView) {
			if (mLinkClickListener != null) {
				mLinkClickListener.onTextLinkClick(textView, mModule);
			}
		}
	}

	private class Hyperlink {
		private InternalURLSpan span;
		private int start;
		private int end;
	}

	public interface OnLinkClickListener {

		public void onTextLinkClick(View textView, Module module);

	}
}
